package org.aurora.core.biz.controller.api;

import org.aurora.core.biz.service.ISlotAssignService;
import org.aurora.core.common.util.HashSlotUtil;
import org.aurora.model.entity.SlotAssignInfo;
import org.aurora.model.response.ClientFetchSchedulerServerAddrResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * todo 后续如果后台与 scheduler server 拆分可以考虑 scheduler server 单独监听端口号
 */
@RestController
public class SchedulerServerController {
    @Resource
    private ISlotAssignService slotAssignService;

    @GetMapping("/fetchSchedulerServerAddr")
    public ClientFetchSchedulerServerAddrResponse fetchSchedulerServerAddr(@RequestParam String appcode) {
        int slot = HashSlotUtil.reHashCode(appcode);
        SlotAssignInfo slotAssignInfo = slotAssignService.querySlotAssignInfoBySlot(slot);

        return new ClientFetchSchedulerServerAddrResponse(slotAssignInfo.getSchedulerIp(), slotAssignInfo.getSchedulerPort());
    }
}
